package top.huxy.boot.config.messageConverter;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpResponse;
import top.huxy.boot.util.obj.BinaryResult;
import top.huxy.boot.util.obj.Result;
import top.huxy.boot.util.obj.ThreadLocalRequestStartTime;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Classname ResultMessageConverter
 * @Description ResultMessageConverter
 * @Date 2021/8/30 19:14
 * @Created by huxy
 */
public class BinaryResultMessageConverter extends AbstractHttpMessageConverter<BinaryResult> {

    public BinaryResultMessageConverter() {
        super(MediaType.ALL);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return clazz == BinaryResult.class;
    }

    @Override
    protected BinaryResult readInternal(Class<? extends BinaryResult> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return null;
    }

    @Override
    protected void writeInternal(BinaryResult result, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        String contentType = null;


        if (outputMessage instanceof ServletServerHttpResponse) {
            HttpServletResponse response = ((ServletServerHttpResponse) outputMessage).getServletResponse();
            contentType = response.getContentType();
        }
        HttpHeaders headers = outputMessage.getHeaders();
        if (contentType != null) {
            headers.set("Content-Type", contentType);
        }

        Result resultResult = result.getResult();
        if (resultResult != null) {
            long start = ThreadLocalRequestStartTime.get();
            long now = System.currentTimeMillis();
            resultResult.setProcessingTime(now - start);
            headers.set("remakes", URLEncoder.encode(JSON.toJSONString(resultResult), "UTF-8"));
            headers.set("Access-Control-Expose-Headers", "remakes");
        }
        byte[] data = result.getData();
        if(data!=null){
            try{
                outputMessage.getBody().write(data);
            }catch (Exception ignored){

            }
        }

//        if (outputMessage instanceof ServletServerHttpResponse) {
//            Result resultResult = result.getResult();
//            if (resultResult != null) {
//                HttpServletResponse response = ((ServletServerHttpResponse) outputMessage).getServletResponse();
//                HttpHeaders headers = outputMessage.getHeaders();
//                String contentType = response.getContentType();
//                if (contentType != null) {
//                    headers.set("Content-Type", contentType);
//                }
//                long start = ThreadLocalRequestStartTime.get();
//                long now = System.currentTimeMillis();
//                resultResult.setProcessingTime(now - start);
//                response.setHeader("remakes", URLEncoder.encode(JSON.toJSONString(resultResult), "UTF-8"));
//                response.setHeader("Access-Control-Expose-Headers", "remakes");
//            }
//        }
//        outputMessage.getBody().write(result.getData());
    }
}
